package com.sgl.gk.service.impl;

import com.sgl.common.core.domain.AjaxResult;
import com.sgl.gk.controller.vo.GkStationConfigurationVo;
import com.sgl.gk.controller.vo.GkStationTypeConfigurationVo;
import com.sgl.gk.domain.GkStationTypeConfiguration;
import com.sgl.gk.mapper.GkStationTypeConfigurationMapper;
import com.sgl.gk.mapper.GkStationTypeEventMapper;
import com.sgl.gk.service.GkStationTypeConfigurationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author:xec
 * @date:2022/10/31 18:50
 **/
@Service
@Slf4j
public class GkStationTypeConfigurationServiceImpl implements GkStationTypeConfigurationService {

    @Autowired
    GkStationTypeConfigurationMapper gkStationTypeConfigurationMapper;

    @Autowired
    GkStationTypeEventMapper gkStationTypeEventMapper;

    @Override
    public AjaxResult list(GkStationTypeConfiguration gkStationTypeConfiguration) {
        List<GkStationTypeConfiguration> list = gkStationTypeConfigurationMapper.list(gkStationTypeConfiguration);
        ArrayList arrayList = new ArrayList<>();
        for (GkStationTypeConfiguration stationTypeConfiguration : list) {
            GkStationTypeConfigurationVo gkStationTypeConfigurationVo = new GkStationTypeConfigurationVo();
            gkStationTypeConfigurationVo.setId(stationTypeConfiguration.getId());
            gkStationTypeConfigurationVo.setStationTypeId(stationTypeConfiguration.getStationTypeId());
            gkStationTypeConfigurationVo.setType(stationTypeConfiguration.getType());
            gkStationTypeConfigurationVo.setVariableAddress(stationTypeConfiguration.getVariableAddress());
            gkStationTypeConfigurationVo.setVariableName(stationTypeConfiguration.getVariableName());
            gkStationTypeConfigurationVo.setDisplayName(stationTypeConfiguration.getDisplayName());
            gkStationTypeConfigurationVo.setDataType(stationTypeConfiguration.getDataType());
            gkStationTypeConfigurationVo.setVariableComments(stationTypeConfiguration.getVariableComments());
            gkStationTypeConfigurationVo.setResultConversion(stationTypeConfiguration.getResultConversion());
            gkStationTypeConfigurationVo.setVariableRemarks(stationTypeConfiguration.getVariableRemarks());
            gkStationTypeConfigurationVo.setDefaultValue(stationTypeConfiguration.getDefaultValue()==null?stationTypeConfiguration.getDefaultValue():(stationTypeConfiguration.getDataType().equals("boolean")?stationTypeConfiguration.getDefaultValue().equals("false")?Boolean.FALSE:Boolean.TRUE:stationTypeConfiguration.getDefaultValue()));
            gkStationTypeConfigurationVo.setZeroValue(stationTypeConfiguration.getZeroValue()==null?"": stationTypeConfiguration.getZeroValue());
            gkStationTypeConfigurationVo.setOneValue(stationTypeConfiguration.getOneValue()==null?"": stationTypeConfiguration.getOneValue());
            gkStationTypeConfigurationVo.setTwoValue(stationTypeConfiguration.getTwoValue()==null?"": stationTypeConfiguration.getTwoValue());
            gkStationTypeConfigurationVo.setThreeValue(stationTypeConfiguration.getThreeValue()==null?"": stationTypeConfiguration.getThreeValue());
            gkStationTypeConfigurationVo.setFourValue(stationTypeConfiguration.getFourValue()==null?"": stationTypeConfiguration.getFourValue());
            gkStationTypeConfigurationVo.setFiveValue(stationTypeConfiguration.getFiveValue()==null?"": stationTypeConfiguration.getFiveValue());
            gkStationTypeConfigurationVo.setDataType(stationTypeConfiguration.getDataType());
            gkStationTypeConfigurationVo.setVariableIndex(stationTypeConfiguration.getVariableIndex());
            gkStationTypeConfigurationVo.setResultConversion(stationTypeConfiguration.getResultConversion());
            gkStationTypeConfigurationVo.setCheckLength(stationTypeConfiguration.getCheckLength());
            gkStationTypeConfigurationVo.setCreateAt(stationTypeConfiguration.getCreateAt());
            gkStationTypeConfigurationVo.setLastModifyAt(stationTypeConfiguration.getLastModifyAt());
            arrayList.add(gkStationTypeConfigurationVo);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",list.size());
        map.put("valueList",arrayList);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult add(GkStationTypeConfiguration gkStationTypeConfiguration) {
        if (gkStationTypeConfiguration.getCheckLength()!=null&&gkStationTypeConfiguration.getCheckLength()>127){
            return AjaxResult.error("检测长度超出范围");
        }
        if (gkStationTypeConfiguration.getVariableAddress()!=null&&gkStationTypeConfiguration.getVariableAddress()>2147483647){
            return AjaxResult.error("变量地址超出范围");
        }
        if (gkStationTypeConfiguration.getCheckLength()!=null&&gkStationTypeConfiguration.getCheckLength()>0&&!gkStationTypeConfiguration.getDataType().equals("boolean")){
            if (gkStationTypeConfiguration.getDefaultValue()!=null&&gkStationTypeConfiguration.getDefaultValue().length()>gkStationTypeConfiguration.getCheckLength()) {
                return AjaxResult.error("默认值不能超过"+gkStationTypeConfiguration.getCheckLength());
            }
        }
        gkStationTypeConfiguration.setCreateAt(new Date());
        gkStationTypeConfiguration.setLastModifyAt(new Date());
        int row = gkStationTypeConfigurationMapper.add(gkStationTypeConfiguration);
        if (row>0) {
            return AjaxResult.success("插入成功");
        }
        return AjaxResult.error("插入失败");
    }

    @Override
    public AjaxResult edit(GkStationTypeConfiguration gkStationTypeConfiguration) {
        if (gkStationTypeConfiguration.getCheckLength()!=null&&gkStationTypeConfiguration.getCheckLength()>127){
            return AjaxResult.error("检测长度超出范围");
        }
        if (gkStationTypeConfiguration.getVariableAddress()!=null&&gkStationTypeConfiguration.getVariableAddress()>2147483647){
            return AjaxResult.error("变量地址超出范围");
        }
        if (gkStationTypeConfiguration.getCheckLength()!=null&&gkStationTypeConfiguration.getCheckLength()>0&&!gkStationTypeConfiguration.getDataType().equals("boolean")){
            if (gkStationTypeConfiguration.getDefaultValue()!=null&&gkStationTypeConfiguration.getDefaultValue().length()>gkStationTypeConfiguration.getCheckLength()) {
                return AjaxResult.error("默认值不能超过"+gkStationTypeConfiguration.getCheckLength());
            }
        }
        int row = gkStationTypeConfigurationMapper.edit(gkStationTypeConfiguration);
        if (row>0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @Override
    public AjaxResult editList(GkStationTypeConfiguration[] gkStationTypeConfigurationList) {
        for (GkStationTypeConfiguration gkStationTypeConfiguration : gkStationTypeConfigurationList) {
            if (gkStationTypeConfiguration.getDefaultValue() == null) {
                gkStationTypeConfiguration.setDefaultValue("");
            }
        }
        int row = gkStationTypeConfigurationMapper.editList(gkStationTypeConfigurationList);
        if (row>0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @Transactional
    @Override
    public AjaxResult removeByIds(Long[] ids) {
        int row = gkStationTypeConfigurationMapper.removeByIds(ids);
        gkStationTypeEventMapper.removeByConfigurationIds(ids);
        if (row>0){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }


}
